import java.util.Scanner;

/**
 * @author gao
 * @date 2025/9/9 11:08
 */

public class Solution {
    public int maxSubarraySumCircular(int[] nums) {
        int n = nums.length, sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }

        int fMax = Integer.MIN_VALUE, gMin = Integer.MAX_VALUE;

        int[] f = new int[n + 1];
        int[] g = new int[n + 1];

        for (int i = 1; i < (n + 1); i++) {
            f[i] = Math.max(f[i - 1] + nums[i - 1], nums[i - 1]);
            fMax = Math.max(f[i], fMax);
            g[i] = Math.min(g[i - 1] + nums[i - 1], nums[i - 1]);
            gMin = Math.min(g[i], gMin);
        }
        return sum == gMin ? fMax : Math.max(fMax, sum - gMin);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < nums.length; i++) {
                nums[i] = in.nextInt();
            }
            Solution solution = new Solution();
            System.out.println(solution.maxSubarraySumCircular(nums));
        }
    }
}